﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class monthapplist : System.Web.UI.Page
    {
        protected string[] orders_Tendency = new string[] { "", "" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_BUSNIESE_NUM] != null)
            {
                string businessNum = Session[DTKeys.SESSION_BUSNIESE_NUM].ToString(); 
                orders_Tendency = GetBusinessOrdersTendency(30, businessNum);
            }
        }


        protected string[] GetBusinessOrdersTendency(int nday, string businessNum) 
        {
            //获取最近n天的订单统计数据
            DateTime startday = DateTime.Now.AddDays(-nday);
            DateTime endDay = DateTime.Now.AddDays(-1);
            string strStartTime = startday.ToString("yyyy-MM-dd") + " 00:00:00.000";
            string strEndTime = endDay.ToString("yyyy-MM-dd") + " 23:59:59.999";
            BLL.sf_appidmanage bll_app = new BLL.sf_appidmanage();
            System.Data.DataSet dsAppList = bll_app.GetList(" businessNum = '"+businessNum+"'");

            BLL.statistics bll_order = new BLL.statistics();
            System.Data.DataSet dsOrders = bll_order.GetOrderTimesDataByBusinessNum(businessNum, strStartTime, strEndTime);

            string[] arr_series = new string[2];
            string xAxis = "";
            for (int i = 0; i < nday; i++)
            {
                xAxis += "'" + startday.AddDays(i).ToString("MM-dd") + "',";
            }
            if (xAxis.Length > 0) 
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);
            }

            string yAxis = string.Empty;
            for (int i = 0 ;i < dsAppList.Tables[0].Rows.Count ;i++)
            {
                System.Data.DataRow r = dsAppList.Tables[0].Rows[i];
                string appNum = r["appid"].ToString();

                string temp_yAxis = string.Empty;
                for (int j = 0; j < nday; j++)
                {
                    string strday = startday.AddDays(j).ToString("yyyy-MM-dd");
                    System.Data.DataRow[] rows = dsOrders.Tables[0].Select(" appNum = '" + appNum + "' and day = '" + strday + "'");

                    if (rows.Length > 0)
                    {
                        temp_yAxis += rows[0]["count"].ToString() + ",";
                    }
                    else
                    {
                        temp_yAxis += "0,";
                    }
                }

                temp_yAxis = temp_yAxis.Length > 0 ? temp_yAxis.Remove(temp_yAxis.Length - 1, 1) : "";
                yAxis += "{ type: 'spline',name:'" + r["appid_name"].ToString() + "', data: [" + temp_yAxis + "] },";
            }

            yAxis = yAxis.Length > 0 ? yAxis.Remove(yAxis.Length - 1 , 1) : "";
            arr_series[0] = xAxis;
            arr_series[1] = yAxis;

            return arr_series;
        }
    }
}